/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.thread;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.client.IUSRequestParams;
import com.apache.database.model.Page;
import com.apache.excp.core.impl.LoadManager;
import com.apache.excp.core.manager.ExportExecl;
import com.apache.excp.core.model.ExportVo;
import com.apache.portal.common.oscache.CacheHelper;
import com.apache.portal.common.util.PortalPubFactory;
import com.apache.rpc.common.LoadRpcService;
import com.apache.tools.ConfigUtil;
import com.apache.tools.DataMap;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.ToolsUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * description: 导出任务线程类
 *
 * @author qihan 创建时间：2017-9-14
 */
public class ExceloutTask implements Callable<Integer> {

    private Map<String, String> params;

    private String objName;

    private LoginUser loginUser;

    private ExportVo vo;

    public ExceloutTask(String objName, Map<String, String> params, LoginUser loginUser,
            ExportVo vo) {
        this.params = params;
        this.objName = objName;
        this.loginUser = loginUser;
        this.vo = vo;
    }

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see java.util.concurrent.Callable#call()
     */
    public Integer call() throws Exception {
        int count = 1;
        try {
            getParams();
            boolean mark = true;
            int j = 1;
            params.put("rows", "500");
            List list = new ArrayList();
            do {
                Map map = params;
                map.put("page", j);
//                map.put("sysPass",  ConfigUtil.getInstance().interfacePass());
                ResultEntity entity = LoadRpcService.service()
                        .doServiceClient(PortalPubFactory.getInstance().getBeanId(params.get("datasource")), "dymicSql",
                                map, PortalPubFactory.getInstance().getRpcInfo("ius"));
                if (null == entity.getEntity()) {
                    mark = false;
                } else {
                    if (entity.getEntity() instanceof Page) {
                        Page page = (Page) entity.getEntity();
                        if (!Validator.isEmpty(page.getPageObjects())) {
                            list.addAll(page.getPageObjects());
                            if (j >= page.getPageCount()) {
                                mark = false;
                            } else {
                                j++;
                            }
                        } else {
                            mark = false;
                        }
                    } else if(entity.getEntity() instanceof List){
                        list=(List<DataMap>) entity.getEntity();
                    }else{
                        mark = false;
                    }
                }
            } while (mark);
            for(Object object: list){
                DataMap dataMap = (DataMap) object;
                Map<String,Object> sm = (Map<String, Object>) dataMap.get(this.objName);
                for(Object o : sm.keySet()){
                    String str = sm.get(o).toString();
                    if(str.contains("\r")||str.contains("\n")||str.contains("\r\n")){
//                        str = str.replaceAll("\r","");
//                        str = str.replaceAll("\n","");
                        str = str.replaceAll("[\\t\\n\\r]","");
                        sm.put(o.toString(),str);
                    }
                }
            }
            ExportExecl exp = LoadManager.getInstance().getExp();
//            vo.setFilePath("");
//            vo.setKey("");
//            vo.setNewFileName(params.get(""));
//            vo.setTemplateName("");
            vo.setParams(objName, list);
            exp.exportData(vo);//downloadFile
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return count;
    }

    private void getParams() {
        params.put("resultType", "objInfo");
        params.put("sysPass", ConfigUtil.getInstance().interfacePass());
        String pageName = params.get("pageName");
        String formName = params.get("formName");

        if (StrUtil.isNull(formName) || StrUtil.isNull(pageName)) {
            params.put("sqlKeyId", "sqlKeyId");
            params.put("modelTypes", "s_" + objName);
            return;
        }
        //BaseOsCache oscache = OsCacheOtherManager.getInstance().getBaseOsCache("param_manager_data_", 60 * 60 * 24);
        String key = formName + "_" + pageName;

        Map map = (Map) CacheHelper.getInstance()
                .getIusParamCache(key);//.getCache("").getCacheObjectByKey(key);
        if (ToolsUtil.isEmpty(map)) {
            params.put("sqlKeyId", "sqlKeyId");
            params.put("modelTypes", "s_" + objName);
            return;
        }
        Map resultMap = IUSRequestParams.analyzeParams(map);
        params.putAll(resultMap);
    }

    public static void main(String[] args) {
        String str = "asdasdads\r1231234\r\n13";
        String strr = str.replaceAll("\\t\\n\\r","zxc");
        System.out.println(strr);
    }
}
